import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Register from '../views/Register.vue'

Vue.use(VueRouter)

const routes = [
  {
    /*path：用户输入http://localhost:8080/这个路径的时候
    name:代表这个路径叫login这个名称
    component：组件 当用户访问path路径的时候 跳转到这个组件去
    */
    path: "/",
    name: "login",
    component: () => import('../views/Login.vue')
  },
  {
    path: "/register",
    name: "register",
    component: Register
  },

  {
    //redirect:重定向
    //当我们访问showIndex组件时，会立刻去加载index组件
    path: "/showindex",
    name: "showIndex",
    component: () => import('../views/ShowIndex.vue'),
    children: [
      {
        path: "/index",
        meta: "首页",
        name: "index",
        component: () => import('../views/Index.vue')
      },
      {
        path: "/addteacher",
        meta: "添加讲师",
        name: "addTeacher",
        component: () => import('../views/AddTeacher.vue')
      },
      {
        path: "/updateteacher/:id",//:是参数的意思
        meta: "修改讲师",
        name: "updateTeacher",
        component: () => import('../views/AddTeacher.vue')
      },
      {
        path: "/showteacher",
        meta: "讲师列表",
        name: "showTeacher",
        component: () => import('../views/ShowTeacher.vue')
      },
      {
        path: "/subject",
        meta: "课程分类管理",
        name: "subject",
        component: () => import('../views/Subject.vue')
      },
      {
        path: "/courseinfo",
        meta: "发布课程",
        name: "courseinfo",
        component: () => import('../views/CourseInfo.vue')
      },
      {
        path: "/courseinfo/:id",
        meta: "发布课程",
        name: "courseinfo",
        component: () => import('../views/CourseInfo.vue')
      },
      {
        path: "/chapter/:id",
        meta: "课程大纲",
        name: "chapter",
        component: () => import('../views/Chapter.vue')
      },
      {
        path: "/showcourse",
        meta: "课程列表",
        name: "showcourse",
        component: () => import('../views/ShowCourse.vue')
      }
    ]
  },


  {
    path: "/confirmemail",
    name: "confirmEmail",
    component: () => import('../views/ConfirmEmail.vue')
  },
  {
    path: "/resetpassword",
    name: "resetpassword",
    component: () => import('../views/ResetPassWord.vue')
  },
  {
    path: "/changesuccess",
    name: "ChangeSuccess",
    component: () => import('../views/ChangeSuccess.vue')
  },
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  //就像java里面的拦截器一样他会拦截我们的所有跳转
  let path = to.path;
  console.log(path);
  if (path == "/" || path == "/register" || path == "/confirmemail" || path == "/resetpassword" || path == "/changesuccess") {
    next();
  } else {
    let nickName = sessionStorage.getItem("nickName");
    if (nickName == null) {
      next("/");//回到/路径 回到登录页面
    } else {
      next();
    }
  }
})

export default router
